<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 10:38
 */
namespace app\index\controller;

use app\admin\model\ProxyMoney;
use app\index\model\Mch;
use app\index\model\ProxyWithdraw;
use think\App;
use app\index\model\Proxy as ProxyModel;
use app\index\validate\Proxy as ProxyValidate;
use think\db\Where;

class Proxy extends Base {
    protected $model = null;
    protected $valid = null;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->model = new ProxyModel();
        $this->valid = new ProxyValidate();
    }


    public function index($id=0){

    }

    /**
     * 申请代理
     */
    public function save(){
        $data = request()->post();
        $result = $this->model->addNew(self::$uid,$data['name'],$data['tel'],$out);
        if(!$result){
           return $this->__json(1,$out);
        }
        return $this->__json(0,'申请成功,我们尽快为您审核,请关注审核进度',[
            'last_id'=> $result,
            // '__id'=>self::$uid
        ]);
    }

    /**
     * 申请详情
     * @param int $id
     * @throws \think\Exception\DbException
     */
    public function read(){
        $info = $this->model
            ->where('mid','eq',self::$uid)
            ->find();
        if(!$info){
            return $this->__json(1,'申请不存在');
        }
        return $this->__json(0,'',$info);
    }

    /**
     * 重新申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update(){
        $info = $this->model
            ->where('mid','eq',self::$uid)
            ->find();
        if(!$info){
            return $this->__json(1,'申请不存在');
        }
        if($info['status']!=3){
            return $this->__json(1,'您已经是代理或正在审核中');
        }
        $info->status = 1;
        $info->info = '';
        $info->name = input('name');
        $info->tel = input('tel');
        $info->update_time= 0;
        if(!$info->save()){
            return $this->__json(1,'提交失败');
        }
        return $this->__json(0,'提交成功');
    }

    /**
     * @param int $id
     * @param int $limit
     * @throws \think\exception\DbException
     *
     * @route('proxy/mch/:id','get')
     */
    public function mch($id=0,$limit=10){
        $m = new Mch();
        $list = $m->getListByProxyId($id,'id,username,company,tel,status,create_time,update_time',$limit);
        if(!$list){
            return $this->__json(3,'没有数据');
        }
        return $this->__json(0,'',$list);
    }

    /**
     * @param int $id
     * @param int $limit
     * @throws \think\exception\DbException
     *
     * @route('proxy/revenue/:id','get')
     */
    public function money($id=0,$limit=10){
        $m = new ProxyMoney();
        $list = $m->getListByProxyId($id,'id,name,type,money,create_time',$limit);
        if(!$list){
            return $this->__json(3,'没有数据');
        }
        return $this->__json(0,'',$list);
    }

    /**
     * @param int $id
     * @param string $money
     *
     * @route('proxy/withdraw','post')
     */
    public function withdraw($id=0,$money=''){
        if(!$id){
            return $this->__json(1,'缺少参数');
        }
        $info = $this->model->get($id);
        if(!$info){
            return $this->__json(1,'代理不存在');
        }
        $model = new ProxyWithdraw();
        $res = $model->check($info['id'],$out2);
        if(!$res){
            return $this->__json(1,$out2);
        }
        $name = "代理【{$info['name']}】".'，申请提现';
        $result = $model
            ->addNew($name,$money,$info['id'],$out1);
        if(!$result){
            return $this->__json(1,$out1);
        }
        if(!$result->save()){
            return $this->__json(1,'申请失败');
        }
        return $this->__json(0,'申请成功');
    }

    /**
     *
     * @param int $id
     * @throws \think\exception\DbException
     *
     * @route('proxy/withdraw/:id','get')
     */
    public function withdraw_list($id=0){
        if(!$id){
            return $this->__json(1,'缺少参数');
        }
        $where = new Where();
        $where['proxy_id']=$id;
        $model  = new ProxyWithdraw();
        $list = $model->getList($where,10,'id,name,order_id,money,status,fail_info,create_time');
        if(!$list){
            return $this->__json(3,'暂无记录');
        }
        return $this->__json(0,'',$list);
    }
}